define(['vue','table-col-collapse','api','iview'],function (Vue,TableColCollapse,api,iView) {
    Vue.use(iView)
    var bus = new Vue();
//表格内容
    Vue.component('table-pannel', {
        template: "#table-pannel-template",
        components: {
            TableColCollapse: TableColCollapse
        },
        data: function () {
            return {
                saveLoading: false,
                tempParentNum: '',
                addPosition: false,
                deletePos: false,
                //表格数据
                columnsData: [],
                tableData: [],
                tempTableData: [],
                tempEditData: [],
                departmentName: '',
                tempArr: [],
                // 编辑对象
                tempEditObj: {},
                // 获取部门上级时使用的临时对象
                tempObj: {},
                taxModel: false,
                regularityModel: false,
                commissionModel: false,
                tempEditID: '',
                isAddStatus: true,
                editParentNum: '',
                isFlag: true
            }
        },
        methods: {
            showAdd: function () {
                this.isAddStatus = true
                this.addPosition = true
            },
            initData: function () {
                var that = this
                api.GetOrganizaitonNodes({companyId: localStorage.companyId})
                    .done(function (res) {
                        if (res.IsSuccess) {
                            that.tableData = res.ResultObject
                            that.tempTableData = res.ResultObject.slice()
                        }
                    })
            },
            handleSave: function () {
                var that = this
                var postObj = {}
                this.getTreeNodeNum(this.tableData, this.tempEditData[this.tempEditData.length - 1])
                postObj = {
                    num: this.tempEditID,
                    parentNum: this.tempEditData.length === 0 ? localStorage.companyId : this.editParentNum,
                    department: this.departmentName,
                    value: this.departmentName,
                    label: this.departmentName,
                    tax: this.taxModel,
                    regularity: this.regularityModel,
                    commission: this.commissionModel,
                    children: []
                };
                if (this.isAddStatus) {
                    delete postObj.num
                }
                this.isFlag = true
                this.saveLoading = true;
                api.SaveOrganization(postObj)
                    .done(function (res) {
                        if (res.IsSuccess) {
                            if (that.isAddStatus) {
                                that.addNode(res.ResultObject.parentNum, that.tableData, res.ResultObject)
                                that.tableData = that.tableData.slice()
                            } else {
                                if (that.tempEditObj.parentNum === res.ResultObject.parentNum) {
                                    that.editNode(that.tableData, res.ResultObject)
                                    that.tableData = that.tableData.slice()
                                } else {
                                    that.delTreeNode(that.tableData)
                                    that.addNode(res.ResultObject.parentNum, that.tableData, res.ResultObject)
                                    that.tableData = that.tableData.slice()
                                }
                            }
                            that.$Message.success("保存成功")
                            that.handleCancel()
                        } else {
                            that.$Message.error(res.ErrMsg)
                        }
                    })
                    .fail(function () {
                        that.$Message.error('保存错误');
                    })
                    .always(function () {
                        that.saveLoading = false;
                    })
            },
            editNode: function (data, obj) {
                for (var i = 0; i < data.length; i++) {
                    if (data[i].num === obj.num) {
                        data[i].department = obj.department
                        data[i].value = obj.value
                        data[i].label = obj.label
                        data[i].tax = obj.tax
                        data[i].regularity = obj.regularity
                        data[i].commission = obj.commission
                        return
                    }
                    var children = data[i].children.length > 0 ? data[i].children : false;
                    if (children) this.editNode(children, obj)
                }
            },
            addNode: function (parent, data, obj) {
                if (parent === localStorage.companyId) {
                    this.tableData.push(obj)
                    return
                }
                for (var i = 0; i < data.length; i++) {
                    if (data[i].num === parent && this.isFlag) {
                        data[i].children.push(obj);
                        if (data[i].isCollapse) {
                            var currentTableIndex = -1;
                            for (var j = 0; j < this.tableData.length; j++) {
                                if (this.tableData[j].num === data[i].num) {
                                    currentTableIndex = j;
                                }
                            }
                            if (currentTableIndex !== -1) {
                                var tempArr = this.tableData.slice(); // copy array
                                tempArr.splice(currentTableIndex + data[i].children.length, 0, obj);
                                this.tableData = tempArr;
                            }
                        }
                        this.isFlag = false
                        return

                    }
                    var children = data[i].children.length > 0 ? data[i].children : false;
                    if (children) this.addNode(parent, children, obj)
                }
            },
            handleCancel: function () {
                this.tempEditData = "";
                this.departmentName = "";
                this.taxModel = false;
                this.regularityModel = false;
                this.commissionModel = false;
                this.addPosition = false
            },
            handleDelOk: function () {
                var that = this
                api.DeleteOrganization({"departmentId": this.tempEditObj.num})
                    .done(function (res) {
                        if (res.IsSuccess) {
                            that.delTreeNode(that.tableData)
                            that.$Message.success("删除成功")
                        } else {
                            that.$Message.error(res.ErrMsg)
                        }
                    })
            },
            getTreeNodeNum: function (data, value) {
                for (var i = 0; i < data.length; i++) {
                    if (data[i].value === value) {
                        this.editParentNum = data[i].num
                        return
                    }
                    this.getTreeNodeNum(data[i].children, value)
                }
            },
            delTreeNode: function (data) {
                for (var i = 0; i < data.length; i++) {
                    if (data[i].num === this.tempEditObj.num) {
                        var parent = data[i].parentNum;
                        var num = data[i].num;
                        if (data[i].children.length > 0 && data[i].isCollapse) {
                            data.splice(i, this.getTreeLength(data[i].children) + 1)
                        } else {
                            data.splice(i, 1)
                        }
                        this.delChildrenNode(parent, num);
                        return
                    }
                }
            },
            delChildrenNode: function (parentNum, currentNum) {
                for (var i = 0; i < this.tableData.length; i++) {
                    if (this.tableData[i].num === parentNum) {
                        var children = this.tableData[i].children;
                        for (var j = 0; j < children.length; j++) {
                            if (children[j].num === currentNum) {
                                children.splice(j, 1)
                                this.tableData[i].children = children;
                                return;
                            }
                        }
                    }
                }
            },
            getTreeLength: function (data) {
                var treeNodeLength = 0;
                for (var i = 0; i < data.length; i++) {
                    treeNodeLength++;
                    if (data[i].hasOwnProperty('children') && data[i].isCollapse) {
                        treeNodeLength += this.getTreeLength(data[i].children);
                    }
                }
                return treeNodeLength;
            }
            ,
            editTreeNode: function (data, node) {
                for (var i = 0; i < data.length; i++) {
                    if (data[i].children.length > 0 || data[i].parentNum === localStorage.companyId) this.tempObj = data[i]
                    if (data[i].parentNum === localStorage.companyId) this.tempEditData = []
                    if (data[i].num === node.num) return
                    var children = data[i].hasOwnProperty('children') && data[i].children.length > 0
                        ? data[i].children
                        : false
                    if (children) {
                        if (data[i].children.length > 0) this.tempEditData.push(this.tempObj.value)
                        this.editTreeNode(children, node)
                    }
                }
            }
            ,
            handelAddDepartmentName: function (data) {
                for (var i = 0; i < data.length; i++) {
                    if (data[i].hasOwnProperty('parentNum') && data[i].parentNum !== null) {
                        this.tempCascader.push(data[i].department)
                    }

                }
            }
            ,
            initColumns: function () {
                var that = this;
                this.columnsData = [
                    {
                        title: '编号',
                        key: 'num'
                    },
                    {
                        title: '部门名称',
                        key: 'department',
                        render: function (h, params) {
                            return h(TableColCollapse, {
                                props: {
                                    params: params,
                                    tableData: that.tableData,
                                    equalsKey: 'num',
                                    originTableData: that.tempTableData
                                },
                                on: {
                                    'table-data-change': function (newData) {
                                        that.tableData = newData;
                                    }
                                }
                            }, [
                                h('span', params.row.department)
                            ]);
                        }
                    },
                    {
                        title: '操作',
                        key: 'handle',
                        render: (function (h, params) {
                            return h('div', [
                                h('Tooltip', {
                                    attrs: {
                                        placement: 'top',
                                        content: '编辑'
                                    }
                                }, [
                                    h('span', {
                                        attrs: {
                                            class: 'editStaff',
                                            type: 'primary'
                                        },
                                        on: {
                                            click: function () {
                                                that.isAddStatus = false;
                                                that.tempEditObj = params.row
                                                that.tempEditData = [];
                                                that.tempEditID = params.row.num;
                                                that.taxModel = params.row.tax;
                                                that.regularityModel = params.row.regularity;
                                                that.commissionModel = params.row.commission;
                                                that.departmentName = params.row.label;
                                                that.editTreeNode(that.tableData, params.row)
                                                that.addPosition = true
                                            }
                                        }
                                    }, [
                                        h('img', {
                                            attrs: {
                                                src: '../../images/edit-defaut.png'
                                            }
                                        })
                                    ])
                                ]),
                                h('Tooltip', {
                                    attrs: {
                                        placement: 'top',
                                        content: '删除'
                                    }
                                }, [
                                    h('span', {
                                        attrs: {
                                            class: 'cancelStaff',
                                            type: 'primary'
                                        },
                                        on: {
                                            click: function () {
                                                that.tempEditObj = params.row
                                                that.deletePos = true
                                            }
                                        }
                                    }, [
                                        h('img', {
                                            attrs: {
                                                src: '../../images/move-defaut.png'
                                            }
                                        })
                                    ])
                                ])
                            ])
                        })

                    }
                ]
            }
        },
        created: function () {
            this.initData();
            this.initColumns();
        }
    });
    var app = new Vue({
        el: "#app",

    })
})
